package com.googlecode.h0v3rk4rt.core;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.googlecode.h0v3rk4rt.graphics.Multiplayer;
import com.googlecode.h0v3rk4rt.net.MultiplayerServer;

public class h0v3rk4rt extends JFrame {

	/**
	 * DEFAULT UID
	 */
	private static final long serialVersionUID = 1L;
	private MenuPanel menuPanel;
	private BufferedImage buffer, bgImage, starImage, helpbgImage1,
	helpbgImage2, helpbgImage3, helpPanel1, helpPanel2, helpPanel3;
	private BufferedImage[] helpPanels;
	private Graphics2D bufferg;
	private int curItem, curPanel = 0;
	private float angle = 0;
	private Boolean helpMode = false;

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new h0v3rk4rt("h0v3rk4rt - Team ANAND");
			}
		});
	}

	// constructor, creates new menu instance
	public h0v3rk4rt(String title) {
		super(title);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(800, 600);
		setVisible(true);
		menuPanel = new MenuPanel();
		addKeyListener(menuPanel);
		add(menuPanel);
		setupMenu();
		(new rotateThread()).start();
	}

	// sets up menu by setting up background and marker images
	public void setupMenu() {

		curItem = 0;
		buffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
		bufferg = (Graphics2D) buffer.getGraphics();
		helpPanels = new BufferedImage[3];
		try {
			bgImage = ImageIO.read(new File("resource/menuBackground.png"));
			helpbgImage1 = ImageIO.read(new File("resource/menuBackground1.png"));
			helpbgImage2 = ImageIO.read(new File("resource/menuBackground2.png"));
			helpbgImage3 = ImageIO.read(new File("resource/menuBackground3.png"));
			helpPanel1 = ImageIO.read(new File("resource/helpPanel1.png"));
			helpPanel2 = ImageIO.read(new File("resource/helpPanel2.png"));
			helpPanel3 = ImageIO.read(new File("resource/helpPanel3.png"));
			starImage = ImageIO.read(new File("resource/menuStar.png"));
			helpPanels[0] = helpPanel1;
			helpPanels[1] = helpPanel2;
			helpPanels[2] = helpPanel3;
		} catch (IOException e) {
			e.printStackTrace();
		}
		bufferg.drawImage(bgImage, 0, 0, null);
	}

	// thread rotates marker star
	public class rotateThread extends Thread {
		public void run() {
			while (true) {
				angle += .1;
				menuPanel.repaint();
				try {
					Thread.sleep(75);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	// main displayable panel, keeps track of actions
	private class MenuPanel extends JPanel implements KeyListener {
		/**
		 * Default UID
		 */
		private static final long serialVersionUID = 1L;

		public MenuPanel() {
			setSize(800, 600);
		}

		public void paintComponent(Graphics g) {
			if (!helpMode) {
				bufferg.drawImage(bgImage, 0, 0, null);
				AffineTransform xform = AffineTransform.getRotateInstance(
						angle, 10 + 30, 10 + curItem * 90 + 28);
				xform.concatenate(AffineTransform.getTranslateInstance(10,
						10 + curItem * 90));
				bufferg.drawImage(starImage, xform, this);
			} else {
				if (curItem == 0)
					bufferg.drawImage(helpbgImage1, 0, 0, null);
				else if (curItem == 1)
					bufferg.drawImage(helpbgImage2, 0, 0, null);
				else if (curItem == 2)
					bufferg.drawImage(helpbgImage3, 0, 0, null);
				bufferg.setColor(Color.black);
				bufferg.drawImage(helpPanels[curPanel%3], 75, 75, null);
			}
			g.drawImage(buffer, 0, 0, null);
		}

		public void keyPressed(KeyEvent e) {
			int keyCode = e.getKeyCode();
			if (!helpMode) {
				if (keyCode == 38 && curItem > 0) {
					curItem--;
					repaint();
				}

				else if (keyCode == 40 && curItem < 2) {
					curItem++;
					repaint();
				} else if (keyCode == 10) {
					if (curItem % 3 == 0)
						play();
					else if (curItem % 3 == 1) {
						helpMode = true;
						curItem = 0;
						curPanel = 0;
					} else if (curItem % 3 == 2)
						System.exit(0);
				}
			} else { // in helpMode
				if (keyCode == 10) {
					if (curItem % 3 == 0) {
						helpMode = false;
						curItem = 0;
					} else if (curItem % 3 == 1) {
						if (curPanel == 0)
							curPanel = 2;
						else
							curPanel--;
					} else if (curItem % 3 == 2) {
						curPanel++;
					}
				} else if (keyCode == 37 && curItem > 0) {
					curItem--;
					repaint();
				} else if (keyCode == 39 && curItem < 2) {
					curItem++;
					repaint();
				}
			}
		}

		public void keyReleased(KeyEvent e) {

		}

		public void keyTyped(KeyEvent e) {

		}

	}

	private void play() {
		System.out.println("YOU PRESSED PLAY!");
		String hostname = "";
		String myIP;
		try {
			myIP = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			myIP = "[ERROR]";
		}
		hostname = JOptionPane.showInputDialog("Please enter the server IP address\n" +
				"If nobody has started a server, type server to make this computer the server.\n" +
				"(In that case, the server's local address will be " + myIP + ".)");

		this.setVisible(false);
		this.dispose();
		if(hostname.startsWith("server")) {
			MultiplayerServer test = new MultiplayerServer();
			test.start();
			Multiplayer.startDrivingGame("127.0.0.1", this);
		} else if(hostname != "") Multiplayer.startDrivingGame(hostname, this);
	}
	
	public void startAgain() {
		System.out.println("STARTING AGAIN DAWG");
		Multiplayer.shutdown = false;
		this.pack();
		this.setVisible(true);
		this.setSize(800,600);
	}
	
	public void startAgain(String s) {
		if(s != null) JOptionPane.showMessageDialog(new JFrame(), s);
		startAgain();
	}

}